/***************************************************************************
 *   Copyright (C) 2014 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.1
import QtQuick.Dialogs 1.1


Rectangle {
    id: screenTimerList
    anchors.fill:parent

    FocusScope {
        id: scope

        property alias color: screenTimerList.color
        x: screenTimerList.x; y: screenTimerList.y
        width: screenTimerList.width; height: screenTimerList.height
    }


    //Delegate definition of the list entries
    Component {
        id: timerDelegate
        Item {
            id: timerDelegateItem
            width: parent.width; height: delegateTextArea.height + 8 * screenScale
            clip: true

            Row{
                spacing: 4 * screenScale
                Text {
                    id: idTimerChannelNumber
                    text: channelNumber
                    horizontalAlignment: Text.AlignHCenter
                    font.pixelSize: 18 * screenScale
                    color: "white"
                    width: 40 * screenScale
                    maximumLineCount: 1
                }

                Rectangle {
                    width: 18 * screenScale
                    height: width
                    radius: width * 0.5
                    color: recording ? "red" : "black"
                    border.color: "white"
                    border.width: 2 * screenScale
                }

                Column {
                    id: delegateTextArea
                    Text {
                        text: file
                        color: "white"
                        font.pixelSize: 18 * screenScale
                    }
                    Text {
                        text:  startTime + " - " + stopTime
                        color: "white"
                        font.pixelSize: 18 * screenScale
                    }
                }
            }

            MessageDialog {
                id: timerInfoDialog
                visible: false
                title: qsTr( "Timer Information" )
                Component.onCompleted: visible = false
                onAccepted: {
                    visible = false
                }
            }

            MouseArea {
                id: mouseAreaDelegate
                z: 1
                hoverEnabled: false
                anchors.fill: parent

                onClicked: {
                    console.log("Index clicked: " + index )
                    if ( index >= 0 )
                    {
                        listTimerList.currentIndex = index
                    }
                    focus: true
                }

//                onPressAndHold: {
//                    console.log("Index long clicked: " + index )
//                    if ( index >= 0 )
//                    {
//                        listTimerList.currentIndex = index
//                    }
//                    focus: true

//                    timerInfoDialog.title = "" + channelNumber

//                    timerInfoDialog.text = "<b>" + file + "</b><br>"
//                                                   + startTime + " - " + stopTime + "<br>"
//                                                   + channelEpgDescription
//                    timerInfoDialog.visible = true
//                }

            }
        }
    }

    //Display
    Column {
        id: columnTimerList
        anchors.centerIn: parent

        ListView {
            id: listTimerList
            Rectangle {
                color: "black"
                anchors.top: parent.top
                anchors.topMargin: 0
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                z: -1
            }

            model: TimerListModel
            delegate: timerDelegate

            width: mainWindow.width
            height: mainWindow.height
            transformOrigin: Item.Center
            focus: true

            onCountChanged: {
                console.log("Listview: Count changed: " + count )
            }
            onVisibleChanged: {
                console.log("Listview: Visible changed: " + visible )
            }
        }
    }
}
